import { axiosMock, delayFn, randArrItem } from '@/services/axiosMock'
import { DEFAULT_PAGE_SIZE } from '@/utils/pagination'
import type * as TDetail from '../BatchAddOrigRouteDrawer/interface'
import type * as TList from '../interface'

axiosMock('default', mock => {
  // 获取原始路由列表
  mock.onPost(`/tmsapi/original_route/list`).reply(
    delayFn<TList.IOrigRouteListRes>(600, ({ data }) => {
      return {
        success: true,
        data: {
          total: 2500,
          records: Array(+data.size || DEFAULT_PAGE_SIZE)
            .fill(null)
            .map<TList.IOrigRouteListItem>((_item, index) => {
              const count = index + 1
              return {
                originalRouteId: +`${data.page}${count}`,
                waybillNo: randArrItem([`运单号-${count}`], 0.1),
                channelNo: randArrItem([`渠道单号-${count}`], 0.1),
                merchantServers: randArrItem([`服务商-${count}`], 0.1),
                originalRoute: randArrItem(
                  [
                    `原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由原始路由-${count}`,
                  ],
                  0.1,
                ),
                occurDate: randArrItem([`2023-11-16 12:12:12`], 0.1),
                collectDate: randArrItem([`2024-11-17 12:12:12`], 0.1),
                status: randArrItem([0, 1]),
              }
            }),
        },
      }
    }),
  )

  // 批量新增原始路由
  mock.onPost(`/tmsapi/original_route/batchAdd`).reply(
    delayFn<TDetail.IBatchAddOrigRouteRes>(600, ({ data }) => {
      const routeResList = (data as TDetail.IBatchAddOrigRouteReqItem[]).map<TDetail.IBatchAddOrigRouteResItem>(
        (item, index) => {
          const resultType = randArrItem([1, 2, 3] as const)
          return {
            resultType,
            resultDesc: { 1: '导入成功', 2: '识别错误', 3: '该路由已存在' }[resultType],
            waybillNo: resultType === 2 ? null : item.waybillNo,
            merchantServers: resultType === 2 ? null : `服务商-${index}`,
            channelNo: resultType === 2 ? null : `渠道单号-${index}`,
          }
        },
      )
      const successNum = routeResList.filter(d => d.resultType === 1).length
      return {
        success: true,
        data: {
          routeResList,
          totalNum: routeResList.length,
          successNum,
          failNum: routeResList.length - successNum,
        },
      }
    }),
  )

  // 删除原始路由
  mock.onPost(`/tmsapi/original_route/del`).reply(
    delayFn(600, () => {
      return { success: true }
    }),
  )
})
